package com.liaogui.web.verify;

import com.liaogui.order.api.domain.ActInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 校验奖品信息
 * @author liaogui
 * @date 2025/2/27 22:47
 */
@Component
@Order(1)
@Slf4j
@RequiredArgsConstructor
public class PrizeInfoVerifyHandler implements VerifyHandler {

    private final RedisTemplate<String, Object> redisTemplate;

    @Override
    public void operator(ActInfo actInfo) {
        log.info("执行奖品校验处理器");
        String actId = "ACT202502281645";

        // 从redis拿到奖品信息
        // 奖品id集合
        String prizeIdKey = "DRAW:PRIZE:" + actInfo.getActId();
        List<Object> prizeIds = redisTemplate.opsForList().range(prizeIdKey, 0, -1);
        if (CollectionUtils.isEmpty(prizeIds)) {
            throw new RuntimeException("奖品信息不存在");
        }
        actInfo.setPrizeIds(prizeIds);

        // 奖品详细信息
        Map<String, Map<Object, Object>> prizeInfos = new HashMap<>();
        for (Object prizeId : prizeIds) {
            String prizeInfoKey = "DRAW:PRIZE:" + actInfo.getActId() + ":" + prizeId;
            Map<Object, Object> entries = redisTemplate.opsForHash().entries(prizeInfoKey);
            prizeInfos.put((String)prizeId, entries);
        }

        actInfo.setPrizeInfos(prizeInfos);

    }
}
